package com.khaled.protclass.model.util;

import java.util.HashMap;

public class UtilArray
{
	public static void normalize(Double[] properties, boolean sigmoid)
	{
		if (sigmoid)
		{
			for (int i = 0; i < properties.length; i++)
			{
				properties[i] = 1 / (1 + Math.exp(-properties[i]));
			}
		}
		else
		{
			double min = getMin(properties);
			double max = getMax(properties);

			for (int i = 0; i < properties.length; i++)
			{
				double upper = properties[i] - min;
				double lower = (max - min);

				if (lower == 0.0 || upper == 0.0)
				{
					properties[i] = 0.0;
				}
				else
				{
					properties[i] = UtilNumber.round((upper / (double) lower), 3);
				}
			}

		}

	}

	public static double getMax(Double[] properties)
	{
		double max = -Double.MAX_VALUE;

		for (int i = 0; i < properties.length; i++)
		{
			if (properties[i] > max)
			{
				max = properties[i];
			}
		}

		return max;
	}

	public static double getMin(Double[] properties)
	{
		double min = Double.MAX_VALUE;

		for (int i = 0; i < properties.length; i++)
		{
			if (properties[i] == null)
			{
				System.out.println(properties[i]);
			}

			if (properties[i] < min)
			{
				min = properties[i];
			}
		}
		return min;
	}

	public static HashMap<String, Double> normalize(HashMap<String, Double> original)
	{
		HashMap<String, Double> result = new HashMap<String, Double>();

		double min = getMin(original.values().toArray(new Double[original.size()]));
		double max = getMax(original.values().toArray(new Double[original.size()]));

		for (String key : original.keySet())
		{
			double upper = original.get(key) - min;
			double lower = max - min;

			Double value = 0.0;
			if (!(lower == 0.0 || upper == 0.0))
			{
				value = UtilNumber.round((upper / (double) lower), 3);
			}

			result.put(key, value);
		}

		return result;
	}

	public static Double[] sum(Double[] row1, Double[] row2)
	{
		if (row1.length != row2.length)
		{
			return null;
		}

		Double[] result = new Double[row1.length];

		for (int i = 0; i < result.length; i++)
		{
			result[i] = (row1[i] == null ? 0 : row1[i]) + (row2[i] == null ? 0.0 : row2[i]);
		}
		return result;

	}

	public static Double[] divideBy(Double[] row, int number)
	{
		if (number == 0)
		{
			return null;
		}

		Double[] result = new Double[row.length];

		for (int i = 0; i < result.length; i++)
		{
			result[i] = row[i] / (double) number;
		}

		return result;
	}

	public static StringBuffer getArrayAsString(Double[] row)
	{
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < row.length; i++)
		{
			result.append(row[i]).append(",");
		}
		return result;
	}

	public static void initialize(Double[] row, double d)
	{
		for (int i = 0; i < row.length; i++)
		{
			row[i] = d;
		}

	}

	public static void initialize(Double[][] matrix, double d)
	{
		for (int i = 0; i < matrix.length; i++)
		{
			for (int j = 0; j < matrix[i].length; j++)
			{
				matrix[i][j] = d;
			}
		}

	}

	public static void initialize(int[] row, int d)
	{
		for (int i = 0; i < row.length; i++)
		{
			row[i] = d;
		}
	}

	public static String getArrayAsString(int[] row)
	{
		StringBuffer result = new StringBuffer();

		for (int i = 0; i < row.length; i++)
		{
			result.append(row[i]).append(",");
		}

		return result.toString();
	}

}
